package kr.saeildamil.androidstudy_second.fragment;

import kr.saeildamil.androidstudy_second.R;
import kr.saeildamil.androidstudy_second.fragment.common.CounterFragment01;
import kr.saeildamil.androidstudy_second.fragment.common.CounterFragment02;
import kr.saeildamil.androidstudy_second.fragment.common.TextFragment;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class FragmentManagerTest extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch20_fragmentmanagertest);
	}

	public void mOnClick(View v){
		FragmentManager fm = getFragmentManager();
		Fragment fragment = fm.findFragmentById(R.id.fragmentmanangertest_frame);
		
		switch(v.getId()){
		case R.id.fragmentmanangertest_addbtn:
			if(fragment == null){
				FragmentTransaction ft = fm.beginTransaction();
				CounterFragment02 cf = new CounterFragment02();
				ft.add(R.id.fragmentmanangertest_frame, cf,"counter");
				ft.commit();
				
				//fm.beginTransaction().add(R.id.ch20_fragmgntest_frame, cf, "counter").commit();
				
			}else{
				Toast.makeText(this, "이미 프래그먼트가 존재합니다", Toast.LENGTH_LONG).show();
			}
			break;
		case R.id.fragmentmanangertest_removebtn:
			if(fragment == null){
				Toast.makeText(this, "삭제할 프래그먼트가 없음", Toast.LENGTH_LONG).show();
			}else{
				fm.beginTransaction().remove(fragment).commit();
			}
			break;
		case R.id.fragmentmanangertest_replacebtn:
			if(fragment == null){
				Toast.makeText(this, "교체할 프래그먼트가 없음", Toast.LENGTH_LONG).show();
			}else{
				if(fragment.getTag().equals("counter")){
					fm.beginTransaction().replace(R.id.fragmentmanangertest_frame, new TextFragment(), "text").commit();
				}else{
					fm.beginTransaction().replace(R.id.fragmentmanangertest_frame, new CounterFragment02(), "counter").commit();
				}
			}
				break;
		case R.id.fragmentmanangertest_hidebtn:
			if(fragment == null){
				Toast.makeText(this, "프래그먼트가 없음", Toast.LENGTH_LONG).show();
			}else{
				if(fragment.isHidden()){
					fm.beginTransaction().show(fragment).commit();
				}else{
					fm.beginTransaction().hide(fragment).commit();
				}
			}
			break;
		}
	}
}
